package com.twinkle.abmsspringboot.controller;

import com.twinkle.abmsspringboot.entity.Contacts;
import com.twinkle.abmsspringboot.entity.UserInfo;
import com.twinkle.abmsspringboot.service.ContactsService;
import com.twinkle.abmsspringboot.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * ABMS - SpringBoot
 * <p>controller 用户处理控制器</p>
 *
 * @author Twinkle
 * @version 1.0.1
 * @since 30 Nov. 2020
 * @since 2 Aug. 2021
 */
@Slf4j
@Controller
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private ContactsService contactsService;

    @PostMapping("/signIn")
    public String signIn(@RequestParam("loUsername") String loUsername,
                         @RequestParam("loPassword") String loPassword,
                         HttpSession session) {
        log.info("receive input: loUsername = " + loUsername + " : loPassword = " + loPassword);
        UserInfo userInfo = userInfoService.signIn(loUsername, loPassword);
        if (userInfo != null) {
            List<Contacts> contactsList = contactsService.selectContact(userInfo.getUserinfo_id());
            for (Contacts contacts : contactsList) {
                log.debug(contacts.toString());
            }
            log.info("get: " + userInfo.toString());
            session.setAttribute("contactsList", contactsList);
            session.setAttribute("userinfo", userInfo);
            log.info("login -> success");
            return "main";
        } else {
            log.info("login -> failure");
            return "redirect:/";
        }
    }

    @PostMapping(value = "/signUp")
    public String signUp(@RequestParam("reUsername") String reUsername,
                         @RequestParam("rePassword") String rePassword,
                         HttpSession session) {
        log.info("input: reUsername -> " + reUsername + " : rePassword -> " + rePassword);
        boolean isSignUp = userInfoService.signUp(reUsername, rePassword);
        if (isSignUp) {
            log.info("register success");
            UserInfo userInfo = userInfoService.signIn(reUsername, rePassword);
            session.setAttribute("listAll", contactsService.selectContact(userInfo.getUserinfo_id()));
            session.setAttribute("userinfo", userInfo);
            return "redirect:main";
        } else {
            log.info("register failure");
        }
        return "redirect:/";
    }

    @RequestMapping(value = "/signOut")
    public String sign_out(HttpSession session) {
        log.info("prepare -> sign-out");
        UserInfo userInfo = (UserInfo) session.getAttribute("userinfo");
        log.info("get: " + userInfo.getUsername());
        session.removeAttribute("userinfo");
        session.removeAttribute("contactsList");
        log.info("sign-out -> success");
        return "redirect:/";
    }

    @PostMapping(value = "/updateUser")
    public String updateUserInfo(@RequestParam("uUserid") Integer uUserid,
                                 @RequestParam("uUsername") String uUsername,
                                 @RequestParam("uPassword") String uPassword) {
        log.info("prepare -> update-userinfo");
        UserInfo userInfo = new UserInfo(uUserid, uUsername, uPassword);
        log.info("get: " + userInfo.toString());
        boolean isUpdate = userInfoService.updateUserInfo(userInfo);
        if (isUpdate) {
            log.info("update-userinfo -> success");
        } else {
            log.info("update-userinfo -> failure");
        }
        return "redirect:main";
    }
}
